use chrono::{DateTime, Utc, serde::ts_seconds, Local};
use serde::{Serialize, Deserialize};
use std::fmt::{Display, Formatter, Result};

#[derive(Debug, Serialize, Deserialize)]
pub struct Task {
    pub text: String,
    #[serde(with="ts_seconds")]
    pub created_at: DateTime<Utc>,
}

impl Display for Task {
    fn fmt(&self, f: &mut Formatter) -> Result<> {
        let created_at = self.created_at.with_timezone(&Local).format("%F %H:%M");
        write!(f, "{:<10}[{}]", self.text, created_at)
    }
}

impl Task {
    pub fn new(text: String) -> Task {
        let created_at = Utc::now();
        Task {
            text,
            created_at,
        }
    }
}